
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
//https://leetcode.cn/problems/balanced-binary-tree/
class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return height(root) > 0;
    }
    public int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = height(root.left);
        if (leftH < 0) return leftH;
        int rightH = height(root.right);
        if (rightH < 0) return rightH;
        
        if (Math.abs(leftH-rightH) > 1) {
            return -1;
        }else {
            return Math.max(leftH,rightH)+1;
        }
    }
}